﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace mlnx_os_sysdump_analyzer
{
    class CommandHistory : IDisposable
    {
        private DataTable commandsListTable = null;
        private bool KEEP_RUNNING;
        private string OUTPUT_DIR_PATH;
        private string commandSet;
        //
        //----------------------------------------------------
        //
        public CommandHistory(ref bool KEEP_RUNNING, ref string OUTPUT_DIR_PATH, string commandSet)
        {
            this.KEEP_RUNNING = KEEP_RUNNING;   
            this.OUTPUT_DIR_PATH = OUTPUT_DIR_PATH;
            this.commandSet = commandSet;
            // create a DataTable
            commandsListTable = new DataTable();
            // add columns
            commandsListTable.Columns.Add("Date", typeof(string));
            commandsListTable.Columns.Add("Command", typeof(string));
        }
        //
        //----------------------------------------------------
        //
        public DataTable getCommandHistory()
        {
            return commandsListTable;
        }
        //
        //----------------------------------------------------
        //
        public void scanInfo()
        {
            string path = null, fname = "";
            if (commandSet.Equals("root"))
            {
                path = Methods.FindFileRec(OUTPUT_DIR_PATH, "root_cli_history");
                fname = "root_cli_history";
            }
            else
            {
                path = Methods.FindFileRec(OUTPUT_DIR_PATH, "monitor_cli_history");
                fname = "monitor_cli_history";
            }

            if (path != null)
            {
                scanCommandHistory(path);
            }
            else
            {
                Item itm = new Item("Error", "Couldn't locate the file: " + fname);
                commandsListTable.Rows.Add(itm.itemName, itm.content);
            }
        }
        //
        //----------------------------------------------------
        //
        private void scanCommandHistory(string srcFilePath)
        {
            if (srcFilePath == null)
                return;
            StreamReader sr = new StreamReader(srcFilePath);
            try
            {
                Item tmpItem = null;
                while (KEEP_RUNNING && sr.Peek() >= 0)
                {
                    // read line
                    string line = sr.ReadLine();
                    try
                    {
                        tmpItem = new Item();
                        // date
                        string tmp = Regex.Split(line, " ")[1];
                        tmpItem.itemName = tmp.Substring(0,4) + "/" 
                            + tmp.Substring(4,2) + "/"
                            + tmp.Substring(6,2) + " "
                            + tmp.Substring(8,2) + ":"
                            + tmp.Substring(10,2) + ":"
                            + tmp.Substring(12,2);
                        // command
                        line = sr.ReadLine();
                        tmpItem.content = line;
                        commandsListTable.Rows.Add(tmpItem.itemName, tmpItem.content);
                    }//try
                    catch
                    {
                    }
                }
            }//try
            catch 
            {
            }
            sr.Dispose();
        }
        //
        //----------------------------------------------------
        //

        public void Dispose()
        {
            if (commandsListTable != null)
            {
                commandsListTable.Dispose();
            }
        }
    }
}
